package com.hardson.toolkit.util;

import java.util.ArrayList;
import java.util.List;

public class HtmlUtil {

    private String content = "";
    private final List<TagBean> commands = new ArrayList<>();

    public static String htmlTag(String text) {
        if (text.length() > 6 && text.substring(0, 6).toLowerCase().startsWith("<html>")) {
            return text;
        }
        return String.join(text, "<html>", "</html>");
    }

    public static String bodyTag(String text, boolean parentTags) {
        String aText = String.join(text, "<body>", "</body>");
        if (parentTags) {
            aText = htmlTag(aText);
        }
        return aText;
    }

    public static String lineWrap(String plainText) {
        return "<html><font style=\"Writing-mode:tb-rl;Text-align:center;\">" +
                plainText +
                "</font></html>";
    }

    public HtmlUtil font(String face, String color, Integer size) {
        String replace0 = (face == null || (face = face.trim()).isEmpty()) ? "" : String.join(face, "face=\"", "\"");
        String replace1 = (color == null || (color = color.trim()).isEmpty()) ? ""
                : String.join(color.trim(), "color=\"", "\"");
        String replace2 = size == null ? "" : String.join(size.toString(), "size=\"", "\"");
        String prefix = String.format("<font %s %s %s>", replace0, replace1, replace2);

        this.commands.add(TagBean.of(prefix, "</font>"));
        return this;
    }

    public HtmlUtil color(String color) {
        return this.font("", color, null);
    }

    public HtmlUtil face(String face) {
        return this.font(face, "", null);
    }

    public HtmlUtil fontSize(Integer fontSize) {
        return this.font("", "", fontSize);
    }

    public HtmlUtil html() {
        this.commands.add(TagBean.of("<html>", "</html>"));
        return this;
    }

    public HtmlUtil body() {
        this.commands.add(TagBean.of("<body>", "</body>"));
        return this;
    }

    public HtmlUtil underLine() {
        this.commands.add(TagBean.of("<u>", "</u>"));
        return this;
    }

    public static HtmlUtil of(String text) {
        HtmlUtil hu = new HtmlUtil();
        hu.content = text;
        return hu;
    }

    @Override
    public String toString() {
        if (this.commands.isEmpty()) {
            return this.content;
        }
        StringBuilder startTags = new StringBuilder();
        StringBuilder endTags = new StringBuilder();
        int idx = this.commands.size() - 1;
        for (TagBean tb : this.commands) {
            startTags.append(this.commands.get(idx--).startTag);
            endTags.append(tb.endTag);
        }
        return startTags.append(this.content).append(endTags).toString();
    }

    private static class TagBean {
        private String startTag = "";
        private String endTag = "";

        static TagBean of(String startTag, String endTag) {
            TagBean tb = new TagBean();
            tb.startTag = startTag;
            tb.endTag = endTag;
            return tb;
        }
    }
}
